Public Class frmImportUnitsFromForms
    'form for importing units from forms

#Region "Attributes"
#Region "Private attributes"
    Private MoDfrmImportUnitsFromForms As ClsDfrmImportUnitsFromForms = Nothing 'control object for this form
    Private MoAction As ClsAction = New ClsAction(GeAction.kkNoAction) 'action for this form
    Private MeResult As GeActionResult = GeActionResult.kkOk  'result of this form
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of form

    Private MoMarkedUnits As Dictionary(Of String, String) = Nothing 'units with formular paths for import
#End Region
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting form settings before its using
    Private Function InitControl() As GeActionResult
        SetReadyForUse(False) 'disabling form
        Try
            StartPosition = FormStartPosition.CenterParent
            MinimizeBox = False
            WindowState = FormWindowState.Normal
            Size = New Size(700, 400)
            MinimumSize = New Size(800, 500)
            ShowInTaskbar = False
            ShowIcon = True
            Icon = ConvertImageToIcon(imlFormImages.Images("FORMICON"))
            Text = "Importovanie formularov"

            AcceptButton = btnOk
            CancelButton = btnCancel

            Dim LoRepositoryItemButtonEdit As DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit = New DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
            LoRepositoryItemButtonEdit.Name = "MyRepositoryItemButtonEdit"
            AddHandler LoRepositoryItemButtonEdit.ButtonClick, AddressOf RepositoryItemButtonEditButtonClicked
            ucnGridCard.tlTreeList.RepositoryItems.Add(LoRepositoryItemButtonEdit)

            SetReadyForUse(True) 'enabling form
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
#End Region
#Region "Private properties"
    Private ReadOnly Property FocusedRow() As DevExpress.XtraTreeList.Nodes.TreeListNode
        Get
            Return ucnGridCard.tlTreeList.FocusedNode()
        End Get
    End Property
#End Region
#End Region
#Region "Events handler"
#Region "frmImportUnitsFromForms - Events handler"
    Private Sub frmImportUnitsFromForms_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        ucnGridCard.tlTreeList.SetFocusedNode(ucnGridCard.tlTreeList.GetNodeByVisibleIndex(0))
        SetEnable()
    End Sub
    Private Sub frmImportUnitsFromForms_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'user closed via the forms control box close button (X) or via Me.Close - I set Me.DialogResult to Windows.Forms.DialogResult.OK when leaving form in successful situation
        If e.CloseReason = System.Windows.Forms.CloseReason.UserClosing AndAlso Me.DialogResult = Windows.Forms.DialogResult.Cancel Then
            MeResult = GeActionResult.kkCancel
        End If
        'since sometimes form is going to close and I dont know why, here is place for fixing the problem :-)
        Select Case e.CloseReason
            Case CloseReason.None
                e.Cancel = True
        End Select
    End Sub
#End Region
#Region "Buttons - Events handler"
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If MoMarkedUnits.Count > 0 Then
            MoAction.SetData(MoMarkedUnits, "UNITSFORIMPORT")
            MeResult = GeActionResult.kkOk
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        MeResult = GeActionResult.kkCancel
        Me.Close()
    End Sub
#End Region
#Region "ucnGridCard - Events handler"
    Private Sub ucnGridCard_MMSettings() Handles ucnGridCard.MMSettings
        ucnGridCard.tlTreeList.OptionsView.ShowIndicator = False
        ucnGridCard.tlTreeList.OptionsSelection.EnableAppearanceFocusedCell = False
        ucnGridCard.tlTreeList.OptionsSelection.EnableAppearanceFocusedRow = True
        ucnGridCard.tlTreeList.OptionsView.ShowColumns = True
        ucnGridCard.tlTreeList.OptionsView.ShowCheckBoxes = False
        ucnGridCard.tlTreeList.OptionsView.ShowRoot = False
    End Sub
    Private Sub ucnGridCard_MMCreateDataSource() Handles ucnGridCard.MMCreateDataSource
        'ATTENTION: keep the same order here and during adding rows to DataSource
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(0).FieldName = "RowID"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(1).FieldName = "ParentRowID"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(2).FieldName = "Enabled"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(3).FieldName = "Selected"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(4).FieldName = "UnitName"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(5).FieldName = "HumanUnitName"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(6).FieldName = "Path"

        ucnGridCard.tlTreeList.Columns(5).VisibleIndex = 1
        ucnGridCard.tlTreeList.Columns(4).VisibleIndex = 2
        ucnGridCard.tlTreeList.Columns(6).VisibleIndex = 3
        '"RowID" AND "ParentRowID" ARE UNIQUE IDENTIFIERS FOR TREELIST AND THESE COLUMNS ARE INVISIBLE IN TREELIST COZ THEY ARE SET TO PROPERTIES KeyFieldName and ParentFieldName PROPERTIES OF TREELIST
    End Sub
    Private Sub ucnGridCard_MMColumnProperties(ByRef Column As DevExpress.XtraTreeList.Columns.TreeListColumn) Handles ucnGridCard.MMColumnProperties
        Try
            Select Case Column.FieldName
                Case "UnitName"
                    Column.Caption = "Progr. nazov formularu"
                    Column.OptionsColumn.AllowEdit = False
                Case "HumanUnitName"
                    Column.Caption = "Anal. nazov formularu"
                    Column.OptionsColumn.AllowEdit = False
                    Column.SortOrder = SortOrder.Ascending
                Case "Path"
                    Column.Caption = "Importovany formular"
                    Column.OptionsColumn.AllowEdit = True
                    Column.ColumnEdit = ucnGridCard.tlTreeList.RepositoryItems("MyRepositoryItemButtonEdit")
                Case Else
                    'Column.Caption will be the same like Column.FieldName
                    Column.VisibleIndex = -1
                    Column.OptionsColumn.AllowEdit = False
            End Select
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ucnGridCard_MMFillDataSource(ByVal Item As Object, ByRef DataSource As Collection) Handles ucnGridCard.MMFillDataSource
        Try
            Dim LiI As Integer = 0
            For Each LoDataRow As DataRow In MoDfrmImportUnitsFromForms.AModOneVersion.DSaModVersion.UnitsTable.Rows
                LiI = LiI + 1
                DataSource.Add(New ClsUcnGridUnboundItemUnitsForImportExport(0 + LiI, -1, True, False, LoDataRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName), LoDataRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName), ""))
            Next
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ucnGridCard_MMCustomDrawNodeCell(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles ucnGridCard.MMCustomDrawNodeCell
        If e.Node("Path") <> "" Then
            e.Appearance.BackColor = Color.Green
        End If
    End Sub
    Private Sub ucnGridCard_MMRowDoubleClicked(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode) Handles ucnGridCard.MMRowDoubleClicked
        btnOk_Click(Me, Nothing)
    End Sub
    Private Sub ucnGridCard_MMValueChanged(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal Column As DevExpress.XtraTreeList.Columns.TreeListColumn, ByVal NewValue As Object, ByVal OldValue As Object) Handles ucnGridCard.MMValueChanged
        SetEnable()
    End Sub
    Private Sub ucnGridCard_MMValueValidating(ByVal Item As Object, ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal Column As DevExpress.XtraTreeList.Columns.TreeListColumn, ByRef Value As Object, ByRef Valid As Boolean, ByRef ErrorText As String) Handles ucnGridCard.MMValueValidating
        If Column.FieldName = "Path" Then
            Valid = True
        End If
    End Sub
#End Region
#End Region
#Region "Functions"
    'gets all marked versions
    Private Function GetMarkedUnits() As Dictionary(Of String, String)
        Dim LoMarkedUnits As Dictionary(Of String, String) = New Dictionary(Of String, String)

        For Each LoNode As DevExpress.XtraTreeList.Nodes.TreeListNode In ucnGridCard.tlTreeList.Nodes
            If LoNode("Path") <> "" Then LoMarkedUnits.Add(LoNode("UnitName"), LoNode("Path"))
        Next

        Return LoMarkedUnits
    End Function
    'opens open dialog for choosing a path to form
    Private Function ChoosePath() As String
        Dim LsResultPath As String = ""

        OpenFileDialog.InitialDirectory = IO.Path.GetDirectoryName(MoDfrmImportUnitsFromForms.AmodPath)
        OpenFileDialog.FileName = ""
        OpenFileDialog.Filter = "Subory .Designer.vb(*.Designer.vb)|*.Designer.vb"
        OpenFileDialog.Title = "Otvorenie formulara (Designer cast) pre import"
        If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            LsResultPath = OpenFileDialog.FileName
        End If

        Return LsResultPath
    End Function
#End Region
#Region "Subs"
    'set enable/disable for controls
    Private Sub SetEnable()
        MoMarkedUnits = GetMarkedUnits()

        btnOk.Enabled = MoMarkedUnits.Count > 0
    End Sub
    'sets MbReadyForUse attribute and sets Enable property of form
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling form
    End Sub
    'sub for processing click on the 3 dots button on repository item DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
    Private Sub RepositoryItemButtonEditButtonClicked(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs)
        Dim LsPathToForm As String = ChoosePath()
        If LsPathToForm <> "" Then
            ucnGridCard.tlTreeList.FocusedNode.SetValue(ucnGridCard.tlTreeList.FocusedColumn, LsPathToForm)
            'ucnGridCard.tlTreeList.ActiveEditor.EditValue = LsPathToForm - I cant use this, coz I lost focus to open dialog so theres no ActiveEditor!!!
            ucnGridCard.tlTreeList.PostEditor()
            'since there no event for this situation I call manually refreshing of controls
            SetEnable()
        Else
            'do nothing, previous value of cell remains
        End If
    End Sub
#End Region

    '#######################################
    'execute function
    Public Function Execute(ByRef OoAction As ClsAction) As GeActionResult
        Try
            MoAction = OoAction
            '---------------------------------------------
            'ACTION BEFORE ACTION
            '---------------------------------------------

            '---------------------------------------------
            'ACTION
            '---------------------------------------------
            Select Case MoAction.Action
                Case GeAction.kkamodImportUnitsFromForms
                    MoDfrmImportUnitsFromForms = MoAction.GetData("CLSDFRMIMPORTUNITSFROMFORMS")
                    ucnGridCard.Init(Nothing)
                    'SetEnable()

                    Me.ShowDialog()
            End Select
            '---------------------------------------------
            'ACTION AFTER ACTION
            '---------------------------------------------

            '---------------------------------------------
            Return MeResult
            '---------------------------------------------

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    '#######################################

End Class